/*
 * Copyright (c) 2017,重庆阿莫比科技有限公司,All Rights Reserved.
 */
package com.wowocai.btchina;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

/**
 * JDBC配置
 * 
 * @author 刘建成
 */
@Configuration
public class JdbcConfig {

    @Autowired
    private DataSource dataSource;
    //
    // @SuppressWarnings("unchecked")
    // @Bean
    // @ConfigurationProperties(prefix = "spring.datasource")
    // public DataSource dataSource() {
    // try {
    // Class<DataSource> type = (Class<DataSource>) Class.forName(dataSourceType);
    // return DataSourceBuilder.create().type(type).build();
    // } catch (Exception e) {
    // return DataSourceBuilder.create().type(com.zaxxer.hikari.HikariDataSource.class).build();
    // }
    // }

    // @Autowired
    // private Environment env;
    //
    // @Bean
    // public DataSource dataSource() {
    // // DruidDataSource dataSource = new DruidDataSource();
    // String driver = env.getProperty("spring.datasource.driver-class-name");
    // String url = env.getProperty("spring.datasource.url");
    // String username = env.getProperty("spring.datasource.username");
    // String password = env.getProperty("spring.datasource.password");
    //
    // SimpleDataSource dataSource = new SimpleDataSource(driver, url, username, password);
    // // dataSource.setInitialSize(2);
    // // dataSource.setMaxActive(20);
    // // dataSource.setMinIdle(0);
    // // dataSource.setMaxWait(60000);
    // // dataSource.setValidationQuery("SELECT 1");
    // // dataSource.setTestOnBorrow(false);
    // // dataSource.setTestWhileIdle(true);
    // // dataSource.setPoolPreparedStatements(false);
    // return dataSource;
    // }

    // @Bean
    // public JdbcTemplate jdbcTemplate() {
    // JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource(), true);
    // return jdbcTemplate;
    // }

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {

        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setConfigLocation(resolver.getResource("classpath:/mybatis-config.xml"));
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml"));

        return sqlSessionFactoryBean.getObject();
    }

    /**
     * 事务管理
     * 
     * @return
     */
    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }

    /**
     * @param sqlSessionFactory
     * @return
     */
    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
